var myFixedName = chrome.i18n.getMessage("default_title");
var optionOp = {
	bind: function () {
		var a = this;
		$("#btnSave").click(function () {
			a.save_options()
		});
		$("#btnRuleUpdate").click(function () {
			a.updateRule()
		});
		$("#btnAddUserRule").click(function () {
			a.addUserRule("", "", "")
		});
		$("#btnImportUserRule").click(function () {});
		$("#user_rule_wrap").on("click", '.btn_del', function () {
			var b = $(this).parent().parent();
			b.remove()
		}).on("click", '.btn_test', function () {
			var b = $(this).parent().parent();
			a.testUserRule(b)
		});
		window.addEventListener("message", function (c) {
			if (c.data.ret != null) {
				var b = c.data.ret;
				window.open(b)
			}
		})
	},
	testUserRule: function (d) {
		var g = this.getUserRuleByHtml(d);
		if (this.vaildateUserRule(g)) {
			var b = prompt("缩略图的网址", "");
			if (b) {
				var f = new RegExp(g.srcPattern);
				var a = g.replaceRule;
				if (f && f.test(b)) {
					var c = document.getElementById("sandboxFrame");
					var e = {
						command: "eval",
						content: a.replace(/@/g, b)
					};
					c.contentWindow.postMessage(e, "*")
				} else {
					alert("图片链接匹配不正确")
				}
			}
		} else {
			alert("请先填写完整")
		}
	},
	updateRule: function () {
		$("#ruleUpdateInfo").text("更新中...");
		var a = this;
		G_CONFIG.updateCommonRule({
			callback: function () {
				a.showRuleInfo()
			}
		})
	},
	save_options: function () {
		localStorage.min_width = document.getElementById("min_width").value;
		localStorage.min_height = document.getElementById("min_height").value;
		var c = $("#useTitle").attr("checked");
		var b = $("#useFixedName").attr("checked");
		if (c) {
			localStorage.savePathType = "useTitle"
		} else {
			if (b) {
				localStorage.savePathType = "useFixedName"
			}
		}
		if ($("#pathName").val() == "") {
			$("#pathName").val(myFixedName)
		}
		localStorage.fixedName = $("#pathName").val();
		localStorage.showUrl = $("#showUrl").attr("checked") ? "1" : "0";
		localStorage.useHotkey = $("#useHotkey").attr("checked") ? "1" : "0";
		localStorage.oneOutput = $("#oneOutput").attr("checked") ? "1" : "0";
		this.saveUserRule();
		var a = document.getElementById("status");
		a.style.display = "block";
		a.innerHTML = MSG("settings_already_save", "设置已经保存了！");
		setTimeout(function () {
			a.innerHTML = "";
			a.style.display = "none"
		}, 2000)
	},
	restore_options: function () {
		$("#min_width").val(parseInt(localStorage.min_width || 0));
		$("#min_height").val(parseInt(localStorage.min_height || 0));
		$("#" + localStorage.savePathType).attr("checked", "checked");
		if (localStorage.showUrl == "1" || localStorage.showUrl == undefined) {
			$("#showUrl").attr("checked", "checked")
		}
		if (localStorage.useHotkey == "1" || localStorage.useHotkey == undefined) {
			$("#useHotkey").attr("checked", "checked")
		}
		if (localStorage.oneOutput == "1" || localStorage.oneOutput == undefined) {
			$("#oneOutput").attr("checked", "checked")
		}
		if (localStorage.fixedName == undefined || localStorage.fixedName == "") {
			$("#pathName").val(myFixedName)
		} else {
			$("#pathName").val(localStorage.fixedName)
		}
		this.showRuleInfo();
		this.showUserRuleInfo()
	},
	showRuleInfo: function () {
		var a = $("#bigImageList");
		a.html("");
		$.each(G_CONFIG.getCommonRules(), function (c, d) {
			a.append('<span title="图片替换代码：' + d.replaceRule + '">' + d.site + "</span>");
			a.append(", ")
		});
		try {
			$("#ruleUpdateInfo").text(MSG("settings_rule_last_update_time", "最后更新时间：") + (localStorage.ruleLastUpdateTime == undefined ? MSG("settings_rule_last_update_time_none", "无") : new Date(localStorage.ruleLastUpdateTime).toLocaleString()))
		} catch (b) {
			console.log("ruleUpdateInfo err", b)
		}
	},
	showUserRuleInfo: function () {
		var a = this;
		$("#user_rule_wrap tbody").html("");
		var b = G_CONFIG.getUserRules();
		if (b.length == 0) {
			$("#user_rule_wrap tbody").html(chrome.i18n.getMessage("settings_rules_empty"))
		}
		$.each(b, function (c, d) {
			a.addUserRule(d.site, d.srcPattern, d.replaceRule)
		})
	},
	addUserRule: function (c, f, b) {
		var a = "<tr>";
		var e = "</tr>";
		var g = "<td><textarea placeholder='" + chrome.i18n.getMessage("settings_rule_name") + "'>" + c + "</textarea></td>";
		g += "<td><textarea placeholder='" + chrome.i18n.getMessage("settings_rule_imageUrl_pattern") + "'>" + f + "</textarea></td>";
		g += '<td><textarea placeholder="' + chrome.i18n.getMessage("settings_rule_url_replace_code") + '">' + b + "</textarea></td>";
		g += "<td class='text_center'><span class=\"link_btn btn_test\">" + chrome.i18n.getMessage("settings_rule_test") + '</span> <span class="link_btn btn_export hidden">导出</span> <span class="link_btn btn_del">' + chrome.i18n.getMessage("settings_rule_delete") + "</span></td>";
		var d = a + g + e;
		$("#user_rule_wrap tbody").append(d)
	},
	getUserRuleByHtml: function (e) {
		var b = $(e).find("textarea");
		var c = this.escapeRule($(b[0]).val().trim());
		var d = this.escapeRule($(b[1]).val().trim());
		var a = this.escapeRule($(b[2]).val().trim());
		return {
			site: c,
			srcPattern: d,
			replaceRule: a
		}
	},
	vaildateUserRule: function (a) {
		if (a.site != "" && a.srcPattern != "" && a.replaceRule != "") {
			return true
		}
		return false
	},
	saveUserRule: function () {
		var b = [];
		var a = this;
		$("#user_rule_wrap tbody").find("tr").each(function (c, d) {
			var e = a.getUserRuleByHtml(d);
			if (a.vaildateUserRule(e)) {
				b.push(e)
			}
		});
		G_CONFIG.saveUserRule(b);
		a.showUserRuleInfo();
		console.log("user_rules", b)
	},
	escapeRule: function (a) {
		return a
	},
	unescapeRule: function (a) {
		return a
	}
};
$(function () {
	optionOp.restore_options();
	optionOp.bind()
});